/**
 *@NApiVersion 2.x
 *@NScriptType UserEventScript
 */
define(['N/record', 'N/render', 'N/file'], function (record, render, file) {
    function beforeLoad(context) {
        if (context.type === 'view') {
            try {
                var wo = record.load({
                    type: 'workorder',
                    id: context.newRecord.id,
                    isDynamic: true
                });
                var label = {};

                label.tranid = wo.getValue('tranid');  //工单编号
                var quantity = wo.getValue('quantity');

                label.quantity = quantity;//总数量

                var perQuantity = wo.getValue('custbody_piecepackage');  //每包件数
                var packageQuantity = wo.getValue('custbody_package');
                //余数
                var leftQuantity = null;
                if (perQuantity) {
                    if (!packageQuantity) {
                        packageQuantity = Math.ceil(quantity / perQuantity);
                    }
                    leftQuantity = quantity - perQuantity * (packageQuantity - 1);
                } else {
                    if (!packageQuantity) {
                        packageQuantity = 1;
                    }
                    perQuantity = Math.floor(quantity / packageQuantity);
                    leftQuantity = quantity - perQuantity * (packageQuantity - 1);
                }
                label.packageQuantity = packageQuantity; //包数
                var packages = [];
                for (var i = 1; i <= packageQuantity; i++) {
                    var package = {};
                    package.number = i;
                    if (leftQuantity > 0 && i == packageQuantity) {
                        package.perQuantity = leftQuantity;
                    } else {
                        package.perQuantity = perQuantity;
                    }

                    package.barcode = label.tranid + '/' + i;  //条形码
                    packages.push(package);
                }
                label.packages = packages;

                label.salesorder = wo.getValue('custbody_contractno');//合同号

                var assemblyitemId = wo.getValue('assemblyitem');
                var item = record.load({
                    type: record.Type.ASSEMBLY_ITEM,
                    id: assemblyitemId,
                    isDynamic: true
                });
                label.itemid = item.getValue('itemid'); //产品编号
                label.itemname = item.getValue('displayname');  //产品名称
                label.size = item.getValue('custitem_size');  //尺寸
                label.color = item.getValue('custitem_color');   //颜色
                label.colornumber = item.getValue('custitem_color_number');    //色号
                label.craft = wo.getText('custbody_crafts');  //工艺
                //调用模板文件
                var renderer = render.create();
                renderer.templateContent = file.load(411).getContents();
                renderer.addCustomDataSource({
                    format: render.DataSource.JSON,
                    alias: 'label',
                    data: JSON.stringify(label)
                });
                var pdfFile = renderer.renderAsPdf();
                //PDF文件存放文件夹ID
                pdfFile.folder = 185;
                pdfFile.name = label.tranid +new Date().getTime()+ '.pdf';
                var fileId = pdfFile.save();
                var File = file.load(fileId);
                var pdfurl = File.url;
                var form = context.form;
                form.addButton({
                    id: 'custpage_button',
                    label: '打印工单标签',
                    functionName: "window.open('" + pdfurl + "');"
                });
            } catch (e) {
                log.error('error', JSON.stringify(e));
            }
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});
